package com.linewell.export.service.impl;

import com.linewell.export.service.IImportExcelService;
import com.linewell.export.service.ImportExcelBaseService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: ImportExcelServiceImpl
 * @description:
 * @author: pzhongqaing@linewell.com
 * @since: 2020年09月22日 11:41
 */

@Service
public class ImportExcelServiceImpl extends ImportExcelBaseService implements IImportExcelService {

    @Override
    public List<String> importExcelWithSimple(MultipartFile file) {
        // 已取值的行数
        int rowNum = 0;
        // 列号
        int colNum = 0;
        // 真正有数据的行数
        int realRowCount = 0;
        // 得到工作空间
        Workbook workbook = null;
        try {
            workbook = super.getWorkbookByInputStream(file.getInputStream(), file.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (workbook == null) {
            return null;
        }
        // 得到工作表
        Sheet sheet = super.getSheetByWorkbook(workbook, 0);
        realRowCount = sheet.getPhysicalNumberOfRows();
        // 列数
        // int colCount = sheet.getRow(0).getPhysicalNumberOfCells();
        List<String> list = new ArrayList<>();
        for(Row row:sheet) {
            if(realRowCount == rowNum) {
                break;
            }
            if(super.isBlankRow(row)) {
                // 空行跳过
                continue;
            }
            if(row.getRowNum() == -1) {
                continue;
            }else {
                if(row.getRowNum() == 0) {
                    // 第一行表头跳过
                    continue;
                }
            }
            rowNum ++;
            // 暂时只用到一列
            colNum = 1;
            String s = this.getCellValue(sheet, row, colNum - 1);
            // 去空
            if (s == null || s.trim().length() < 1) {
                continue;
            }
            list.add(s);
        }
        return list;
    }
}
